package com.ganinfo.codegeneration.controller;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.FileResourceLoader;

import com.ganinfo.common.PathUtil;
import com.itmyx.metadata.database.Dialect;
import com.itmyx.metadata.database.IntrospectedTable;
import com.itmyx.metadata.database.SimpleDataSource;
import com.itmyx.metadata.utils.DBMetadataUtils;
import com.itmyx.metadata.utils.JavaBeansUtil;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.setting.Setting;

/**
 * @ClassName: MainController
 * @Description: 代码生成器主入口
 * @author: myx
 * @date: 2017年2月25日 下午12:11:38
 */
public class MainController {
	
	private static Setting setting = new Setting("config.properties");
	
	private static String pakageName="com.ganinfo";
	private static String businessName="sys";
	
	
	
	public static void main(String[] args) throws IOException, SQLException {
		List<IntrospectedTable> tableList = getTableList();//所有表信息
		List<IntrospectedTable> tableList1 = new ArrayList<IntrospectedTable>();//所有表信息
		for (IntrospectedTable table : tableList) {
			if("t_health_s_agency".equals(table.getName())){
				tableList1.add(table);
			}
		}
		generate(tableList);
		
	}
	
	
	public static void generate(List<IntrospectedTable> tableList) throws IOException, SQLException{
		String root = PathUtil.getClassResources() + "template";
		FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
		Configuration cfg = Configuration.defaultConfiguration();
		GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
		Template controllerTem = gt.getTemplate("/controller.txt");//controller模板
		Template serviceTem = gt.getTemplate("/service.txt");//service模板
		Template serviceimplTem = gt.getTemplate("/serviceimpl.txt");//serviceimpl模板
		Template pojoTem = gt.getTemplate("/pojo.txt");//pojo模板
		Template mapperTem = gt.getTemplate("/mapper.txt");//mapper模板
		Template xmlTem = gt.getTemplate("/md.txt");//xml模板
		List<Template> templates=new ArrayList<Template>();
		templates.add(controllerTem);
		templates.add(serviceTem);
		templates.add(serviceimplTem);
		templates.add(pojoTem);
		templates.add(mapperTem);
		templates.add(xmlTem);
		for (IntrospectedTable table : tableList) {//遍历生成代码
			String camelCaseString = JavaBeansUtil.getCamelCaseString(table.getName(), true);
			for (Template template : templates) {
				template.binding("table",table);
				template.binding("basePakageName",pakageName+"."+businessName);
			}
			FileUtil.writeString(controllerTem.render(), new File(PathUtil.getClasspath()+"code/com/ganinfo/"+businessName+"/controller/"+camelCaseString+"Controller.java"), "utf-8");
			FileUtil.writeString(serviceTem.render(), new File(PathUtil.getClasspath()+"code/com/ganinfo/"+businessName+"/service/I"+camelCaseString+"Service.java"), "utf-8");
			FileUtil.writeString(serviceimplTem.render(), new File(PathUtil.getClasspath()+"code/com/ganinfo/"+businessName+"/service/impl/"+camelCaseString+"ServiceImpl.java"), "utf-8");
			FileUtil.writeString(pojoTem.render(), new File(PathUtil.getClasspath()+"code/com/ganinfo/"+businessName+"/model/"+camelCaseString+".java"), "utf-8");
			FileUtil.writeString(mapperTem.render(), new File(PathUtil.getClasspath()+"code/com/ganinfo/"+businessName+"/mapper/"+camelCaseString+"Mapper.java"), "utf-8");
			FileUtil.writeString(xmlTem.render(), new File(PathUtil.getClasspath()+"code/com/ganinfo/"+businessName+"/mapper/"+table.getFirstCharacterLowerClassName()+".md"), "utf-8");
		}
	
	}



	/**
	 * @Title: getTableList
	 * @Description: 获取所有表结构
	 * @return
	 * @throws SQLException
	 * @return: List<IntrospectedTable>
	 */
	public static List<IntrospectedTable> getTableList() throws SQLException {
		SimpleDataSource dataSource = new SimpleDataSource(Dialect.MYSQL,setting.getStr("jdbc_url"),setting.getStr("jdbc_username"),setting.getStr("jdbc_password"));
		DBMetadataUtils dbMetadataUtils = new DBMetadataUtils(dataSource);
		return dbMetadataUtils.introspectTables(dbMetadataUtils.getDefaultConfig());
	}

}
